<?php
/**
 * | ---------------------------------------------------------------------------------------------------
 * | ProjectName: mvc
 * | ---------------------------------------------------------------------------------------------------
 * | Author：johnxu <fsyzxz@163.com>
 * | ---------------------------------------------------------------------------------------------------
 * | Home: https://www.xfjpeter.cn
 * | ---------------------------------------------------------------------------------------------------
 * | Data: 201907202019-07-20
 * | ---------------------------------------------------------------------------------------------------
 * | Desc:
 * | ---------------------------------------------------------------------------------------------------
 */

namespace app\admin\controller;

use app\common\libs\Tree;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;

class Rule extends Base
{
    protected function _initialize()
    {
        $this->model    = model('rule');
        $this->validate = validate('rule');
        parent::_initialize();
    }

    /**
     * 规则列表
     */
    public function index()
    {
        $data     = $this->model->select();
        $dataTree = Tree::getRule($data);

        $this->ok(200, [
            'list'  => $dataTree,
            'total' => 0,
        ]);
    }

    /**
     * 获取规则树状
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public function tree()
    {
        $data     = $this->model->where(['status' => 1])->select();
        $dataTree = Tree::getRuleTree($data);

        $this->ok(200, $dataTree);
    }
}